package com.minglead.springboot.platform.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minglead.springboot.common.crud.BaseController;
import com.minglead.springboot.common.persistence.Page;
import com.minglead.springboot.platform.sys.entity.Log;
import com.minglead.springboot.platform.sys.entity.Role;
import com.minglead.springboot.platform.sys.service.LogService;
import com.minglead.springboot.platform.sys.service.SystemService;
import com.minglead.springboot.platform.sys.utils.ActionResponse;


/**
 * 日志Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/log")
public class LogController extends BaseController {

	@Autowired
	private LogService logService;
	
	@Autowired
	private SystemService systemService;
	
	@RequiresPermissions("sys:log:view")
	@RequestMapping(value = {"list", ""})
	public String list(Log log, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Role> roleList=systemService.getRoleList();
        request.setAttribute("roleList", roleList);
		return "modules/sys/logList";
	}
	@RequiresPermissions("sys:log:edit")
	@RequestMapping(value="loadData")
	public void loadData(HttpServletRequest request, HttpServletResponse response) throws Exception{
	
		String title = request.getParameter("title");
		String beginDate = request.getParameter("beginDate");
		String endDate = request.getParameter("endDate");
		

		
		Log log=new Log();	
		log.setTitle(title);
		
		log.setBeginDate(beginDate);
		log.setEndDate(endDate);	
		
        Page<Log> page = logService.findPage(new Page<Log>(request, response), log); 
		renderGrid(request, response, page);
	}
	@ResponseBody
	@RequestMapping(value="delete")
	public void delete(@Param("id") String id,HttpServletResponse response){			
		ActionResponse r = new ActionResponse(1,"操作成功！");
		if(StringUtils.isEmpty(id)){
			r.setMessage("删除失败,id不能为null");
			this.responseObject(r, response);
			return;
		}
		String[] _id = id.split(",");
		for(String id0:_id){
			logService.delete(id0);
		}
		this.responseObject(r, response);
	}
}
